package ase.eleitweg.common;

import java.util.List;

public class Node implements java.io.Serializable, ComparableByValue<Node> {
	
	private static final long serialVersionUID = 100L;

	private int id;
	private Group group;
	private String name,description;
	private Conditions conditions;
	private Workflow w; //associated workflow
	private int x,y;
	
	public boolean isSame(Node other) {
		if(id == other.getId() && name.endsWith(other.getName())
				&& description.equals(other.getDescription()) && group.getId() == other.getGroup().getId() && group.getName().equals(other.getGroup().getName())) {
			if(this.conditions==null && other.conditions==null) {
				return true;
			}
			if(this.conditions!=null && other.conditions!=null) {
				if(!this.conditions.getTitel().equals(other.conditions.getTitel())) {
					return false;
				}
				
				if(this.conditions.getEdgeCount()!=other.conditions.getEdgeCount()) return false;
				for(Edge e : this.conditions.getEdges()) {
					boolean found = false;
					for(Edge e2 : other.conditions.getEdges()) {
						if(e.getDescription().equals(e2.getDescription()) && e.getToNode().getId() == e2.getToNode().getId()) {
							found = true;
						}
					}
					if(!found) {
						return false;
					}
				}
				
				
				return true;
			}
		}
		return false;
	}
	
	public boolean isSimilar(Node other) {
		if(name.equalsIgnoreCase(other.getName())
				&& description.equals(other.getDescription()) && group.getId() == other.getGroup().getId() && group.getName().equals(other.getGroup().getName())) {
			if(this.conditions==null && other.conditions==null) {
				return true;
			}
			if(this.conditions!=null && other.conditions!=null) {
				if(!this.conditions.getTitel().equals(other.conditions.getTitel())) {
					return false;
				}
				for(Edge e : this.conditions.getEdges()) {
					boolean found = false;
					for(Edge e2 : other.conditions.getEdges()) {
						if(e.getDescription().equals(e2.getDescription()) && e.getToNode().getDescription() == e2.getToNode().getDescription()) {
							found = true;
						}
					}
					if(!found) {
						return false;
					}
				}
				return true;
			}
		}
		return false;
	}

	public Node(int id, Group group, String name, String description,
			Conditions conditions, Workflow w) {
		super();
		this.id = id;
		this.group = group;
		this.name = name;
		this.description = description;
		this.conditions = conditions;
		this.w = w;
		this.x = 0;
		this.y = 0;
	}
	
	public Node(int id, Group group, String name, String description,
			Conditions conditions, Workflow w, int x, int y) {
		super();
		this.id = id;
		this.group = group;
		this.name = name;
		this.description = description;
		this.conditions = conditions;
		this.w = w;
		this.x = x;
		this.y = y;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Conditions getConditions() {
		return conditions;
	}

	public void setConditions(Conditions conditions) {
		this.conditions = conditions;
	}

	/**
	 * Return true if this is an end-activity (an activity that has no possible successors).
	 * @return
	 */
	public boolean isEnd() {
		if(conditions.getEdgeCount() == 0) {
			return true;
		}
		return false;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public Workflow getW() {
		return w;
	}

	public void setW(Workflow w) {
		this.w = w;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Node clone() {
		Node clone = new Node(this.getId(), this.getGroup(), this.getName(), this.getDescription(), null, null);
		Conditions c = new Conditions(clone, this.getConditions().getTitel());
		clone.setConditions(c);
		return clone;
	}
}
